/*
 ******************
 * author: 马丰彦
 * date: 2019-05-08
 * function: 路由配置表
 ******************
 */

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * hidden: true                   如果为true，侧边栏显示，否则是为通用隐藏路由
 * alwaysShow: true               如果为true，将总显示
 *                                如果设置了,路由数量必须大于1
 * redirect: noRedirect           如果设置noRedirect，面包屑不重定向标题
 * name:'router-name'             路由名字必须填写，keep-live使用
 * meta : {
    roles: ['admin']              路由权限唯一标识，在queryUserPower请求接口比对递归查询(roles 一定要为数组！)
    title: 'title'                路由标题，面包屑展示标题
    icon: 'svg-name'              路由图标
    breadcrumb: false             如果为false，面包屑标题隐藏
  }
 */

/**
 * constantRoutes
 * 基本路由，不需要权限筛查
 * 所有路由都将被展示
 */
export const constantRoutes = [
  // 404报错
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 默认重定向主页
  {
    path: '/',
    component: Layout,
    // hidden: true,
    redirect: '/dashboard/index'
    /* children: [{
      path: 'index',
      name: 'Index',
      component: () => import('@/views/dashboard/'),
      meta: {
        title: '业务数据同步概要',
        icon: 'dashboard'
      }
    }] */
  },
  {
    path: '/dashboard',
    component: Layout,
    name: 'Dashboard',
    hidden: true,
    children: [{
      path: 'index',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '概要数据',
        icon: 'dashboard'
      }
    }]
  },

  // 无限嵌套例子
  {
    path: '/nested',
    hidden: true,
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: 'Nested',
      icon: 'nested'
    },
    children: [{
      path: 'menu1',
      component: () => import('@/views/nested/menu1/index'), // Parent router-view
      name: 'Menu1',
      meta: {
        title: 'Menu1'
      },
      children: [{
        path: 'menu1-1',
        component: () => import('@/views/nested/menu1/menu1-1'),
        name: 'Menu1-1',
        meta: {
          title: 'Menu1-1'
        }
      },
      {
        path: 'menu1-2',
        component: () => import('@/views/nested/menu1/menu1-2'),
        name: 'Menu1-2',
        meta: {
          title: 'Menu1-2'
        },
        children: [{
          path: 'menu1-2-1',
          component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
          name: 'Menu1-2-1',
          meta: {
            title: 'Menu1-2-1'
          }
        },
        {
          path: 'menu1-2-2',
          component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
          name: 'Menu1-2-2',
          meta: {
            title: 'Menu1-2-2'
          }
        }
        ]
      },
      {
        path: 'menu1-3',
        component: () => import('@/views/nested/menu1/menu1-3'),
        name: 'Menu1-3',
        meta: {
          title: 'Menu1-3'
        }
      }
      ]
    },
    {
      path: 'menu2',
      component: () => import('@/views/nested/menu2/index'),
      meta: {
        title: 'menu2'
      }
    }
    ]
  },

  // 支持外链形式菜单
  {
    path: 'external-link',
    hidden: true,
    component: Layout,
    children: [{
      path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
      meta: {
        title: 'External Link',
        icon: 'link'
      }
    }]
  }
]

/**
 * asyncRoutes
 * 所有路由需要被权限递归筛查动态获取
 * 再次强调，roles一定要为数组类型！
 */
export const asyncRoutes = [

  // 重定向
  {
    path: '*',
    redirect: '/404',
    hidden: true,
    component: () => import('@/views/404')
  },
  // AAAsetting
  /* {
    path: '/setting',
    component: Layout,
    name: 'Expenses',
    hidden: true,
    // meta: {
    //   roles: ['datamonit-product']
    // },
    children: [{
      path: 'index',
      name: 'expenses',
      component: () => import('@/views/aaasetting/index'),
      meta: {
        roles: ['config-platTable'],
        title: 'AAA配置平台列表',
        icon: 'pingtai'
      }
    }]
  }, */
  // 积分触发管理
  {
    path: '/point',
    component: Layout,
    name: 'Point',
    // meta: {
    //   roles: ['datamonit-product']
    // },
    children: [{
      path: 'index',
      name: 'point',
      component: () => import('@/views/point/index'),
      meta: {
        // roles: ['config-platTable'],
        title: '积分触发管理',
        icon: 'action'
      }
    }]
  },
  // 动作管理
  {
    path: '/actionManage',
    component: Layout,
    hidden: true,
    // meta: { roles: 'integralTrigger' },
    children: [{
      path: 'index/:code/:power',
      name: 'ActionManage',
      component: () => import('@/views/point/components/actionManage'),
      meta: { title: '动作管理', icon: 'point' }
    }]
  },
  // 积分成长值流水
  {
    path: '/river',
    component: Layout,
    name: 'River',
    // meta: {
    //   roles: ['datamonit-product']
    // },
    children: [{
      path: 'index',
      name: 'river',
      component: () => import('@/views/river/index'),
      meta: {
        // roles: ['config-platTable'],
        title: '积分成长值流水',
        icon: 'river'
      }
    }]
  },
  // 会员管理
  {
    path: '/vipManage',
    component: Layout,
    // meta: { roles: 'vip' },
    children: [{
      path: 'index',
      name: 'VipManage',
      component: () => import('@/views/vipManage/index'),
      meta: { title: '会员等级管理', icon: 'vipp' }
    }]
  }
]

const createRouter = () => new Router({
  // mode: 'history', // 服务端渲染
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重定向路由
}

export default router
